﻿using System;
using Bussiness.Interface;
using Bussiness.Service;
using Model;
using Common;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Model;

public partial class Reg : System.Web.UI.Page
{
    IBaseService baseService = new BaseService();
    Users users = new Users();
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Reg_Click(object sender, EventArgs e)
    {
        string user = username.Value;
        string name = Name.Value;
        string password1 = Password.Value;
        string password2 = AginPassword.Value;
        if (inputValue(user,name,password1,password2))
        {
            Users users = new Users()
            {
                UserID = Guid.NewGuid().ToString(), UserName = user,Name=name,
                Password = MD5Encrypt.Encrypt(password2),
                UserType = Enums.UserType.User.ToString(),Time=DateTime.Now
            };
            if (baseService.Insert(users).Data)
            {
                Response.Redirect("Login.aspx", false);
            }
            else
            {
                Res("注册失败");
            }
            
        }
    }
    private bool inputValue(string user,string name,string password1,string password2)//输入验证
    {
        if (user != "")
        {
            Result<List<Users>> listresult = baseService.Select<Users>(p => p.UserName == user);
            if (listresult.Data.Count == 0)
            {
                if (name.Length < 10)
                {
                    if (password1 != "" && password2 != "")
                    {
                        if (password1.Trim() == password2.Trim())
                        {
                            return true;
                        }
                        else
                        {
                            Res("密码不一致");
                        }
                    }
                    else
                    {
                        Res("密码不能为空");
                    }
                }
                else
                {
                    Res("姓名长度不符合");
                }
            }
            else
            {
                Res("用户也存在");
            }
        }
        else
        {
            Res("用户名不能为空");
        }
        return false;
    }
    public void Res(string value)
    {
        Response.Write("<script>alert('"+value+"')</script>");
    }
}